//
//  HOTCreateFreeInfoViewController.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-10-9.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTCreateOrderViewController.h"
#import "ZZZUIScrollView+Extends.h"
#import "ZZZDatePicker.h"
#import "UITextField+extend.h"
#import "HOTPopupView.h"
#import "HOTOrderPreviewViewController.h"

@interface HOTCreateOrderViewController ()<UITextFieldDelegate,UITextViewDelegate>

@property(nonatomic,copy)NSString *workDateStr;
@property(nonatomic,copy)NSString *payDateStr;
@property(nonatomic,copy)NSString *convId;
@property(nonatomic,copy)NSString *targetUid;
@end

@implementation HOTCreateOrderViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil convId:(NSString *)convId targetUid:(NSString *)targetUid
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.convId = convId;
        self.targetUid = targetUid;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNaviTitle:@"発注書"];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_message"]];
    
    
    
    [self initView];
}

-(void)initView {
    
    _publisherBgImageView.image = [_publisherBgImageView.image stretchableImageWithLeftCapWidth:30 topCapHeight:18];
    _remarkBgImageView.image = [_remarkBgImageView.image stretchableImageWithLeftCapWidth:30 topCapHeight:18];
    
    _remarkTextView.font = ZZZ_fontWithName(kHOT_FONT_ROUNDED_X, 16);
    _workDateText.font= ZZZ_fontWithName(kHOT_FONT_ROUNDED_X, 16);
    _payDateText.font= ZZZ_fontWithName(kHOT_FONT_ROUNDED_X, 16);
    _publisherAddrTextView.font= ZZZ_fontWithName(kHOT_FONT_ROUNDED_X, 16);
    
    _remarkTextView.inputAccessoryView = [HOTUtils createInputAccessoryViewWithName:MSG_INPUT_BAR_DONE target:self action:@selector(keyBoardDoneBtnClicked:)];
    _publisherAddrTextView.inputAccessoryView = [HOTUtils createInputAccessoryViewWithName:MSG_INPUT_BAR_DONE target:self action:@selector(keyBoardDoneBtnClicked:)];
    
    
    __unsafe_unretained HOTCreateOrderViewController *blockObj = self;
    __block UITextField *blockWorkDateTextText = _workDateText;
    ZZZDatePicker *_datePicker = [ZZZDatePicker createWithTextField:_workDateText
                                                doneCompletionBlock:^(NSDate *selDate) {
                                                    
                                                    float orgY = _mainScrollView.contentOffset.y - self.keyBoardFrame.size.height < 0? 0 : _mainScrollView.contentOffset.y - self.keyBoardFrame.size.height;
                                                    [_mainScrollView setContentOffset:CGPointMake(0, orgY) animated:YES];
                                                    NSString *dateStr = [ZZZUtils formartDate:selDate withStyle:@"yyyy年MM月dd日"];
                                                    blockObj.workDateStr = [ZZZUtils formartDate:selDate withStyle:@"yyyy-MM-dd"];
                                                    blockWorkDateTextText.text = [HOTUtils getString:dateStr];
                                                    
                                                }
                                              cancelCompletionBlock:^{
                                                  
                                                  float orgY = _mainScrollView.contentOffset.y - self.keyBoardFrame.size.height < 0? 0 : _mainScrollView.contentOffset.y - self.keyBoardFrame.size.height;
                                                  [_mainScrollView setContentOffset:CGPointMake(0, orgY) animated:YES];
                                              }];
    [_datePicker setDatePickerMode:UIDatePickerModeDate];
    
    __block UITextField *blockPayDateTextText = _payDateText;
    _datePicker = [ZZZDatePicker createWithTextField:_payDateText
                                 doneCompletionBlock:^(NSDate *selDate) {
                                     
                                     float orgY = _mainScrollView.contentOffset.y - self.keyBoardFrame.size.height < 0? 0 : _mainScrollView.contentOffset.y - self.keyBoardFrame.size.height;
                                     [_mainScrollView setContentOffset:CGPointMake(0, orgY) animated:YES];
                                     NSString *dateStr = [ZZZUtils formartDate:selDate withStyle:@"yyyy年MM月dd日"];
                                     blockObj.payDateStr =  [ZZZUtils formartDate:selDate withStyle:@"yyyy-MM-dd"];
                                     blockPayDateTextText.text = [HOTUtils getString:dateStr];
                                     
                                 }
                               cancelCompletionBlock:^{
                                   
                                   float orgY = _mainScrollView.contentOffset.y - self.keyBoardFrame.size.height < 0? 0 : _mainScrollView.contentOffset.y - self.keyBoardFrame.size.height;
                                   [_mainScrollView setContentOffset:CGPointMake(0, orgY) animated:YES];
                               }];
    [_datePicker setDatePickerMode:UIDatePickerModeDate];
    
    
    
}

-(void)requestUpdate {
    
    
   
}

#pragma mark 点击返回按钮
-(void)backBtnClicked:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma delegate
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    [_mainScrollView scrollViewToVisible:textField animated:YES];
    
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
    
    if (textField == _priceText) {
        
        if (_priceText.text.length > 0 && [ZZZUtils isNumerical:_priceText.text]) {
            
            NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc] init];
            numFormatter.roundingMode = kCFNumberFormatterRoundCeiling;
            _priceText.text = [numFormatter stringFromNumber:[NSNumber numberWithFloat:[_priceText.text floatValue]]];
        }
        
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    
    float orgY = _mainScrollView.contentOffset.y - self.keyBoardFrame.size.height < 0? 0 : _mainScrollView.contentOffset.y - self.keyBoardFrame.size.height;
    [_mainScrollView setContentOffset:CGPointMake(0, orgY) animated:YES];
    
    return YES;
}

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    
    [_mainScrollView scrollViewToVisible:textView animated:YES];
    
    return YES;
}

-(void)keyBoardDoneBtnClicked:(id)sender {
    
    [_remarkTextView resignFirstResponder];
    [_publisherAddrTextView resignFirstResponder];
    
    float orgY = _mainScrollView.contentOffset.y - self.keyBoardFrame.size.height < 0? 0 : _mainScrollView.contentOffset.y - self.keyBoardFrame.size.height;
    [_mainScrollView setContentOffset:CGPointMake(0, orgY) animated:YES];
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)checkValid {
    
    NSString *errName = @"";
    HOTInputItemType inputType = 0;
    if (_orderNameText.text == nil || _orderNameText.text.length <= 0) {
        
        errName = @"案件名";
        inputType = HOTInputItemText;
    } else if (_priceText.text == nil || _priceText.text.length <= 0) {
        
        errName = @"金額(税抜き)";
        inputType = HOTInputItemText;
    } else if (_publisherNameText.text == nil || _publisherNameText.text.length <= 0) {
        
        errName = @"発注者名(正式名称)";
        inputType = HOTInputItemText;
    }  else if (_zipCodeText.text == nil || _zipCodeText.text.length <= 0) {
        
        errName = @"発注者郵便番号";
        inputType = HOTInputItemText;
    } else if (_publisherAddrTextView.text == nil || _publisherAddrTextView.text.length <= 0) {
        
        errName = @"発注者住所";
        inputType = HOTInputItemText;
    } else if (_publisherPhoneNumText.text == nil || _publisherPhoneNumText.text.length <= 0) {
        
        errName = @"発注者電話番号";
        inputType = HOTInputItemText;
    } else if (_receiverNameText.text == nil || _receiverNameText.text.length <= 0) {
        
        errName = @"受注者名(正式名称)";
        inputType = HOTInputItemText;
    } else if (self.workDateStr == nil || self.workDateStr.length <= 0) {
        
        errName = @"稼働日";
        inputType = HOTInputItemPullDown;
    } else if (self.payDateStr == nil || self.payDateStr.length <= 0) {
        
        errName = @"支払日";
        inputType = HOTInputItemPullDown;
    }
    
    if (errName.length > 0) {
        
        NSString * errMsg = [self getErrMsgWith:errName itemType:inputType];
        
        ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                              message:errMsg
                                                             delegate:nil
                                                    cancelButtonTitle:nil
                                                     otherButtonTitle:ALERT_BTN_OK];
        
        [popupView showInWindow];
        
        return NO;
    }
    
    if(![ZZZUtils isNumerical:_priceText.text]) {
        
        ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                              message:MSG_WRONG_PRICE
                                                             delegate:nil
                                                    cancelButtonTitle:nil
                                                     otherButtonTitle:ALERT_BTN_OK];
        
        [popupView showInWindow];
        return NO;
    }
    
    NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc] init];
    numFormatter.roundingMode = kCFNumberFormatterRoundCeiling;
    _priceText.text = [numFormatter stringFromNumber:[NSNumber numberWithFloat:[_priceText.text floatValue]]];
    
    
    return YES;
}



- (IBAction)previewBtnClicked:(id)sender {
    
    if (![self checkValid]) {
        
        return;
    }
    
    [_orderNameText resignFirstResponder];
    [_publisherNameText resignFirstResponder];
    [_publisherAddrTextView resignFirstResponder];
    [_receiverNameText resignFirstResponder];
    [_remarkTextView resignFirstResponder];
    [_priceText resignFirstResponder];
    [_zipCodeText resignFirstResponder];
    [_publisherPhoneNumText resignFirstResponder];
    [_faxNumText resignFirstResponder];
    [_workDateText resignFirstResponder];
    [_payDateText resignFirstResponder];
    
    
    [_mainScrollView setContentOffset:CGPointZero animated:YES];
    
    NSDictionary *orderInfo = @{@"orderName":_orderNameText.text,
                                @"price":_priceText.text,
                                @"publisherName":_publisherNameText.text,
                                @"publisherZipCode":_zipCodeText.text,
                                @"publisherAddr":_publisherAddrTextView.text,
                                @"publisherPhoneNum":_publisherPhoneNumText.text,
                                @"publisherFAXNum":_faxNumText.text,
                                @"reciverName":_receiverNameText.text,
                                @"workDate":self.workDateStr,
                                @"payDate":self.payDateStr,
                                @"remark":_remarkTextView.text,
                                @"convId":self.convId,
                                @"targetId":self.targetUid
                                };
    
    HOTOrderPreviewViewController *opvc = [[HOTOrderPreviewViewController alloc] initWithNibName:@"HOTOrderPreviewViewController" bundle:nil orderInfo:orderInfo];
    
    __block HOTCreateOrderViewController *tempObj = self;
    opvc.completion = ^(NSString *url, NSString *orderId) {
        
        if (tempObj.completion) {
            
            tempObj.completion(url, orderId);
        }
    };
    
    [self.navigationController pushViewController:opvc animated:YES];
}
@end
